import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
file_path = '全国人口年度数据.xlsx'
data = pd.read_excel(file_path)
years = data['年份']
urban_population = data['城镇人口（万人）']
rural_population = data['乡村人口（万人）']
plt.figure(figsize=(14, 8))
plt.plot(years, rural_population, label='乡村人口', color='r', marker='^', linewidth=2, markersize=6)
plt.plot(years, urban_population, label='城镇人口', color='g', marker='o', linewidth=2, markersize=6)
plt.title('2003年至2023年全国城镇与乡村人口变化折线图', color='#008B8B', fontsize=16, fontweight='bold')
plt.xlabel('年份', color='#008B8B', fontsize=14)
plt.ylabel('人口（万人）', color='#008B8B', fontsize=14)
plt.grid(axis='both', alpha=0.3, linestyle='--')
for i, year in enumerate(years):
    plt.text(year, urban_population[i] + 200, f'{urban_population[i]:.0f}',
             fontsize=9, ha='center', va='bottom', color='g')
    plt.text(year, rural_population[i] - 500, f'{rural_population[i]:.0f}',
             fontsize=9, ha='center', va='top', color='r')
plt.legend(fontsize=12, loc='best')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()